import pool from '../config/database.js';
import logger from '../config/logger.js';

export const PhotoModel = {
  async create(photo) {
    try {
      const [result] = await pool.execute(
        'INSERT INTO photos (title, description, url, created_at) VALUES (?, ?, ?, NOW())',
        [photo.title, photo.description, photo.url]
      );
      return result.insertId;
    } catch (error) {
      logger.error('Error creating photo:', error);
      throw error;
    }
  },

  async findAll() {
    try {
      const [rows] = await pool.execute('SELECT * FROM photos ORDER BY created_at DESC');
      return rows;
    } catch (error) {
      logger.error('Error finding photos:', error);
      throw error;
    }
  },

  async findById(id) {
    try {
      const [rows] = await pool.execute('SELECT * FROM photos WHERE id = ?', [id]);
      return rows[0];
    } catch (error) {
      logger.error('Error finding photo by id:', error);
      throw error;
    }
  },

  async delete(id) {
    try {
      const [result] = await pool.execute('DELETE FROM photos WHERE id = ?', [id]);
      return result.affectedRows > 0;
    } catch (error) {
      logger.error('Error deleting photo:', error);
      throw error;
    }
  }
};